package com.smartsofa.adapter;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.smartsofa.R;
import com.smartsofa.main.DetailNews;

public class NewsListAdapter extends BaseAdapter{
	ViewHolder holder;
	private List<Map<String,Object>> mData;
	private LayoutInflater mInflater;
	private Context mContext;
	
	//咨询的ID
	protected long newsId;
	private String newsurl;
	
    protected final class ViewHolder{            
        public TextView tv_news_title;  //标题
        public TextView tv_news_abstract;  //摘要
        public LinearLayout lay_news;
      }
    
    public NewsListAdapter(Context context, List<Map<String,Object>> data){      
    	this.mInflater = LayoutInflater.from(context); 
		this.mContext = context;
		this.mData = data;
    } 
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mData.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return mData.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		// TODO Auto-generated method stub
		if (convertView == null) {
			holder=new ViewHolder();     
            convertView = mInflater.inflate(R.layout.listview_knowledge, null);        
            
            holder.tv_news_title=(TextView) convertView.findViewById(R.id.tv_news_title);
            holder.tv_news_abstract=(TextView)convertView.findViewById(R.id.tv_news_abstract);
            holder.lay_news=(LinearLayout) convertView.findViewById(R.id.lay_news);
            convertView.setTag(holder); 
        
		}else{
			holder = (ViewHolder) convertView.getTag();
		}
		String news_title=mData.get(position).get("infoTitle").toString();
        holder.tv_news_title.setText(news_title);
        String news_abstract=mData.get(position).get("infoAbstarct").toString();
        holder.tv_news_abstract.setText(news_abstract+"...");
        //获取咨询的ID
        newsId=(Integer) mData.get(position).get("infoId");
        newsurl=(String) mData.get(position).get("infoUrl");
        holder.lay_news.setOnClickListener(new ClickDetailNewsListener(newsId,newsurl));
		return convertView;
	}
	
	class ClickDetailNewsListener implements OnClickListener{
		
		long newsid;
		String newsurl;
		public ClickDetailNewsListener(long newsId,String newsurl){
			this.newsid=newsId;
			this.newsurl=newsurl;
		}

		public void onClick(View arg0) {
			// TODO Auto-generated method stub
//			Toast.makeText(mContext, "你点击了序号为"+newsid+"的新闻!", Toast.LENGTH_SHORT).show();
			Intent intent=new Intent();
			Bundle bundle=new Bundle();
			bundle.putLong("newsid", newsid);
			bundle.putString("newsUrl", newsurl);
			intent.putExtras(bundle);
			intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
			intent.setClass(mContext, DetailNews.class);
			mContext.startActivity(intent);
		}
		
	}

}
